package ru.ifmo.mlarinsky.classificator.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class IOUtil {
    public static final String DATA_FILE_PATH = "ru/ifmo/mlarinsky/classificator/data/";
    public static final String FIVE_YEARS_DATA_FILE_PATH = DATA_FILE_PATH + "5_years_data/";
    public static final String INSTRUMENTS_DATA_FILE_PATH = DATA_FILE_PATH + "instruments/";
     public static final String FIVE_YEARS_INSTRUMENTS_DATA_FILE_PATH = FIVE_YEARS_DATA_FILE_PATH + "instruments/sample/";
   public static final String TEST_INSTRUMENTS_DATA_FILE_PATH = DATA_FILE_PATH + "test/";
    public static final String TEST_INDEXES_DATA_FILE_PATH = TEST_INSTRUMENTS_DATA_FILE_PATH + "indexes/";
    public static final String INDEXES_DATA_FILE_PATH = DATA_FILE_PATH + "indexes/";
    public static final String FIVE_YEARS_INDEXES_DATA_FILE_PATH = FIVE_YEARS_DATA_FILE_PATH + "indexes/";
    public static final String DJ_INDEXES_FILE_PATH = FIVE_YEARS_INDEXES_DATA_FILE_PATH + "/DJ/";
    public static final String SP_INDEXES_FILE_PATH = FIVE_YEARS_INDEXES_DATA_FILE_PATH + "/S&P/";
    public static final String ALL_INDEXES_FILE_PATH = FIVE_YEARS_INDEXES_DATA_FILE_PATH + "/all/";
    public static final String CONF_FOLDER = "conf/";
    public static final String OUTPUT_FOLDER = "output/";
    public static final String LOG_FOLDER = OUTPUT_FOLDER + "log/";
    public static final String XML_FOLDER = OUTPUT_FOLDER + "xml/";
    public static final String INSTRUCTIONS_FOLDER = XML_FOLDER + "instructions/";
    public static final String VECTORS_FOLDER = XML_FOLDER + "vectors/";
    public static final String MAP_FOLDER = OUTPUT_FOLDER + "map/";
    public static final String VECTORS_XML_FILE_PATH = VECTORS_FOLDER + "vectors.xml";
    public static final String INSTRUCTIONS_XML_FILE_PATH = INSTRUCTIONS_FOLDER + "instructions.xml";

    private IOUtil() {
    }

    public static List<String> read(String filePath) {
        List<String> strings = new ArrayList<String>();

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(filePath));
            String nextDataString = reader.readLine();
            while (nextDataString != null) {
                strings.add(nextDataString);
                nextDataString = reader.readLine();
            }
        } catch (IOException e) {
            throw new ClassificatorRuntimeException("Failed to read file " + filePath, e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    throw new ClassificatorRuntimeException("Failed to read file " + filePath, e);
                }
            }
        }
        return strings;

    }

    public static void write(String content, String filePath) {
        System.out.println("Trying to write file " + filePath);

        BufferedWriter writer = null;
        try {
            File file = new File(filePath);
            writer = new BufferedWriter(new FileWriter(file));
            writer.write(content);
        } catch (IOException e) {
            throw new ClassificatorRuntimeException("Failed to write file " + filePath, e);
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    throw new ClassificatorRuntimeException("Failed to write file " + filePath, e);
                }
            }
        }

        checkFileContent(filePath, content);
    }

    public static void checkFileContent(String filePath, String contentStandard) {
        String messagePattern = "File " + filePath + " check failed.";

        String content;
        try {
            List<String> lines = read(filePath);

            StringBuilder contentBuilder = new StringBuilder();
            for (String line : lines) {
                contentBuilder.append(line).append("/n");
            }

            content = contentBuilder.toString();
        } catch (Throwable th) {
            throw new ClassificatorRuntimeException(messagePattern, th);
        }

        if (content == null)
            throw new ClassificatorRuntimeException(messagePattern + " Reason: Empty file.");

        if (!trim(content).equals(trim(contentStandard)))
            throw new ClassificatorRuntimeException(messagePattern + " Reason: Wrong content.");

        System.out.println("File " + filePath + " was successfully written.");
    }

    private static String trim(String s) {
        return s.replaceAll("/n", "").replaceAll("\n", "").replaceAll("\r", "").trim();
    }
}
